<?php

namespace App\Http\Controllers\Auth;

use App\RBAC\Resource;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    /**
     * Defined field for authentication
     *
     * @var string
     */
    public function username()
    {
        return 'name';
    }

    protected function authenticated(Request $request, $user)
    {
        $operations=$user->role->operations;
        $resources_arr=[];
        $menu_arr=[];
        foreach ($operations as $operation) {
            $resources=$operation->resources;
            foreach ($resources as $resource) {
                if($resource->sort_id){
                    $menu_arr[$resource->id]=$resource->toArray();
                }   
            }
            $pids=$resources->pluck('id')->toArray();
            $resources_arr=Resource::select('res_string')->whereIn('id',$pids)->orWhereIn('pid',$pids)->get()->pluck('res_string')->concat($resources_arr);
        }

        $request->session()->put('resources',$resources_arr);

        $resources=[];
        foreach ($menu_arr as $item) {
            echo $item['parent_id'];
            if(!$item['parent_id']){
                $childrens=[];
                foreach ($menu_arr as $citem) {
                  if($item['id']==$citem['parent_id']){
                    $childrens[$citem['sort_id']]=$citem;
                  }  
                }
                ksort($childrens);
                $item['childrens']=$childrens;
                $resources[$item['sort_id']]=$item;
            }  
        }
        $t=ksort($resources);

        
        $menu='';
        foreach ($resources as $value) {
            $childrens=$value['childrens'];
            if(!empty($childrens)){
                $childrens_str='';
                foreach ($childrens as $item) {

                    $childrens_str=$childrens_str.'<a class="dropdown-item menu-'.(str_replace('/','-',$item['res_string'])).'" href="/'.$item['res_string'].'">'.$item['name'].'</a>';
                }
                $menu=$menu.'<li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="'.$value['res_string'].'" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> '.$value['name'].' </a> <div class="dropdown-menu" aria-labelledby="navbarDropdown"> '.$childrens_str.'</div> </li>';
            }else{
                $menu=$menu.'<li class="nav-item"><a class="nav-link menu-'.(str_replace('/','-',$value['res_string'])).'" href="/'.$value['res_string'].'">'.$value['name'].'</a></li>';
            }
            
        }


        session(['menu' => $menu]);
    }
}
